﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickGraph;
using EpidemicSimulator.states;
using EdgeWithAttributes;
namespace EpidemicSimulator.strategies
{
    public class RandomInfectionStrategy : StateChangeStrategy
    {
        #region StateChangeStrategy Members

        public void applyStrategy(UndirectedGraph<SocialNetworkNode, EdgeWithAttributes<SocialNetworkNode>> socialNetwork, AdjacencyGraph<DiseaseModelNode, DiseaseModelEdge> diseaseModelGraph, int numOfUnits)
        {
            DiseaseModelNode infectingState = null;
            foreach (DiseaseModelNode diseaseModelNode in diseaseModelGraph.Vertices)
            {
                if (diseaseModelNode.diseaseStartState)
                {
                    infectingState = diseaseModelNode;
                    break;
                }
            }

            SocialNetworkNode[] nodes = socialNetwork.Vertices.ToArray();
            Random random = new Random();
            while (numOfUnits > 0)
            {
                int r = random.Next(nodes.Length);
                if (!nodes[r].state.stateNode.infected && nodes[r].state.stateNode.stateName != "Immunized")
                {
                    int maxContactsPerStep = nodes[r].state.maxContacsPerStep;
                    int minContactsPerStep = nodes[r].state.minContacsPerStep;
                    nodes[r].state = State.getStateInstance(infectingState.stateClass);
                    nodes[r].state.stateNode = infectingState;
                    nodes[r].state.diseaseModelGraph = diseaseModelGraph;
                    nodes[r].state.node = nodes[r];
                    nodes[r].state.maxContacsPerStep = maxContactsPerStep;
                    nodes[r].state.minContacsPerStep = minContactsPerStep;
                    nodes[r].newState = nodes[r].state;
                    numOfUnits--;
                }
            }
        }

        #endregion
    }
}
